package com.appcheck.recording;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import android.content.res.Resources;
import android.view.View;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import com.appcheck.recording.utils.Logger;
import com.robotium.solo.RobotiumUtils;
import com.robotium.solo.Solo;

@SuppressWarnings("unchecked")
public final class WidgetHelper {
	private static Solo solo;
	private static HashMap widgetMap;
	private static Integer incrementId;
	private static Integer decrementId;
	  
	static{
	   (WidgetHelper.widgetMap = new LinkedHashMap()).put(Integer.valueOf(6), RadioButton.class);
	    widgetMap.put(Integer.valueOf(7), CheckBox.class);
	    widgetMap.put(Integer.valueOf(12), ToggleButton.class);
	    widgetMap.put(Integer.valueOf(5), Button.class);
	    widgetMap.put(Integer.valueOf(10), ImageButton.class);
	    widgetMap.put(Integer.valueOf(11), ImageView.class);
	    widgetMap.put(Integer.valueOf(8), DatePicker.class);
	    widgetMap.put(Integer.valueOf(9), TimePicker.class);
	    widgetMap.put(Integer.valueOf(13), AbsListView.class);
	    widgetMap.put(Integer.valueOf(4), AbsListView.class);
	    widgetMap.put(Integer.valueOf(3), AdapterView.class);
	    widgetMap.put(Integer.valueOf(2), EditText.class);
	    widgetMap.put(Integer.valueOf(1), TextView.class);
	    widgetMap.put(Integer.valueOf(14), WebView.class);
	    widgetMap.put(Integer.valueOf(0), View.class);
	 }
	
	public static void setSolo(Solo paramSolo){
		solo = paramSolo;
	}
	/**
	 * 现在如果获取到的是系统内id也不设为空
	 * @param paramView
	 * @return
	 */
	public static String getResourceName(View paramView){
		String str = null;
	    if (paramView.getId() != -1) {
	      try{
	        if ((str = paramView.getContext().getResources().getResourceName(paramView.getId()).replace(":id/", ".R.id.")).startsWith("android.R.id.")){
	          //Logger.INFO(new StringBuilder("Found internal id: ").append(str).append(".").toString());
	          //str = null;
	        }
	      }
	      catch (Resources.NotFoundException localNotFoundException) {}
	    }
	    return str;
	}
	
	public static int getResourceId(View paramView){
		return paramView.getId();
	}
	
	public static String getViewContent(View paramView){
	    String str = null;
	    if ((paramView instanceof ToggleButton)) {
	    	
	    	ToggleButton tb = (ToggleButton)paramView;
	    	str = tb.isChecked() ? String.valueOf(tb.getTextOff()) : String.valueOf(tb.getTextOn());
	    	
	    } else if ((paramView instanceof TextView)) {
	    	TextView tv = (TextView)paramView;
	    	str = tv.getText().toString();
	    	if ((str != null) && (str.equals(""))) {
	    		str = null;
	    	}
	    }
	    return str;
	}
	/**
	 * get the index of the current window through RobotiumUtils
	 * @param paramView
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static int getCurrentIndexOfView(View paramView){
	    if (solo != null) {
	    	return RobotiumUtils.removeInvisibleViews(solo.getCurrentViews(
	    			(Class)widgetMap.get(Integer.valueOf(getWidgetMapIndexOfView(paramView))))).indexOf(paramView);
	    }
	    Logger.WARN("ViewHelper not initialized - solo is null");
	    return -1;
	}
	
	public static int getIndexInAdapterViewOfView(View paramView){
		//Logger.DEBUG("parent of paramView is " + paramView.getParent().getClass().getSimpleName());
		if(paramView.getParent() instanceof AdapterView){
			return ((AdapterView)paramView.getParent()).getPositionForView(paramView);
		}
		return -1;
	}
	
	public static int getWidgetMapIndexOfView(View paramView){
		Class viewClass = paramView.getClass();
		//遍历控件类型，查看当前的paramView是不是这些类型的子类
		for (Iterator localIterator = widgetMap.entrySet().iterator(); localIterator.hasNext();){
			Map.Entry localEntry = (Map.Entry)localIterator.next();
			// System.out.println(Object.class.isAssignableFrom(ArrayList.class));  //true
			if (((Class)localEntry.getValue()).isAssignableFrom(paramView.getClass())) {
				//Logger.INFO("getWidgetMapIndexOfView(" + viewClass.getSimpleName() + ") " + String.valueOf((Integer)localEntry.getKey()));
				return ((Integer)localEntry.getKey()).intValue();//如果是，则返回对应的key
			}
		}
		return 0;
	}
	
	public static boolean check(View paramView){
	    if (incrementId == null){
	      incrementId = Integer.valueOf(paramView.getResources().getIdentifier("android:id/increment", null, null));
	      decrementId = Integer.valueOf(paramView.getResources().getIdentifier("android:id/decrement", null, null));
	      Logger.DEBUG("incrementId = " + String.valueOf(incrementId) + " the type of incrementId is " + paramView.getResources().getResourceTypeName(incrementId));
	      Logger.DEBUG("decrementId = " + String.valueOf(decrementId) + " the type of decrementId is " + paramView.getResources().getResourceTypeName(decrementId));
	      Logger.DEBUG("id of paramView is " + String.valueOf(paramView.getId()));
	    }
	    if ((paramView.getId() == incrementId.intValue()) || (paramView.getId() == decrementId.intValue())) {
	      return true;
	    }
	    return false;
	}	
  
}
